# -*- coding: utf-8 -*-

import logging
import os
from logging.config import fileConfig

from flask import Flask
from flask import jsonify
from flask_cors import CORS

from config import config
from database import db
from extensions import marshmallow
from vsogame.contril import api


def error_handler(response, *args, **kwargs):
    return jsonify({
        'errorss':  response.description
    }), response.code
def configure_app(app):
    config_name = os.getenv('ENV') or 'default'

    app.config.from_object(config[config_name])

    CORS(app)

    app.register_error_handler(401, error_handler)
    app.register_error_handler(400, error_handler)


def register_extensions(app):
    # database.init_app(app)
    marshmallow.init_app(app)
    db.init_app(app)
    print(os.getcwd())
    logging.config.fileConfig('./logging.conf')


    from  extensions import redis_store, oauth, file_store
    api.configure(
        client_id=app.config.get('PAYMENT_CLIENT_ID'),
        client_secret=app.config.get('PAYMENT_CLIENT_SECRET'),
        endpoint=app.config.get('PAYMENT_ENDPOINT')

    )
    file_store.init_app(app)
    redis_store.init_app(app)
    oauth.init_app(app)
    # sms_provider.init_app(app)
    # gps_provider.init_app(app)
    # if config_name == 'default':
    #     redis_store.init_app(app)
    # db.init_app(app)
    # migrate.init_app(app, db)
    # oauth.init_app(app)
    # sentry.init_app(app)
    # qiniu.init_app(app)
    # fixme oss path 暂时为字符串,后期需要用 aliyun oss 生成
    oss = app.config.get('OSS_PATH', '1')
    # from .models.T import T
    # database.connect()
    # database.database.create_table(T,safe=True)
    # print(T.get())


def register_blueprints(app):
    # from .open_api.ProductsView import ProductsView
    # from .open_api.stock import StocksView
    # from .open_api.service import ServicesView
    # from .open_api.auth import AuthsView
    # from .open_api.shop_cart import ShopCartsView
    # from .open_api.member import MembersView
    # from .open_api.shop_cart_item import ShopCartItemsView
    # from .open_api.fund_channel import FundChannelsView
    # from .open_api.order import OrderResultView ,OrdersView
    # from .open_api.offer import OffersView
    # from .open_api.account_history import AccountHistoriesView

    from auth.api.auth import AuthsView as  avview

    from mall.view.product import ProductsView
    from mall.view.product_category import ProductCategoriesView
    from mall.view.metadata import MetadataView
    from mall.view.resource import ResourcesView
    from mall.view.language import LanguagesView
    from mall.view.member import MembersView
    from mall.view.setting import SettingsView
    from mall.view.shop_cart_item import ShopCartItemsView
    from mall.view.brand import BrandView
    from mall.view.additional_item import AdditionalItemsView
    from mall.view.offer import OfferView
    from mall.view.shop_cart import ShopCartsView
    from mall.view.account import AccountView
    from mall.view.order import OrdersView
    # AccountHistoriesView.register(app)
    # OffersView.register(app)
    # ShopCartItemsView.register(app)
    # MembersView.register(app)
    # OrdersView.register(app)
    # AuthsView.register(app)
    # ServicesView.register(app)
    # ProductsView.register(app)
    # FundChannelsView.register(app)
    # StocksView.register(app)
    # OrderResultView.register(app)
    avview.register(app, route_prefix='v2')
    ProductsView.register(app,route_prefix="v2")
    MetadataView.register(app,route_prefix="v2")
    MembersView.register(app, route_prefix='v2')
    ResourcesView.register(app,route_prefix='v2')
    ProductCategoriesView.register(app,route_prefix="v2")
    LanguagesView.register(app, route_prefix='v2')
    SettingsView.register(app,route_prefix='v2')
    ShopCartItemsView.register(app,route_prefix='v2')
    BrandView.register(app,route_prefix='v2')
    AdditionalItemsView.register(app,route_prefix='v2')
    OfferView.register(app,route_prefix='v2')
    ShopCartsView.register(app, route_prefix='v2')
    AccountView.register(app, route_prefix='v2')
    OrdersView.register(app,route_prefix='v2')

    from payment.views.fund_channle import FundChannelView
    FundChannelView.register(app, route_prefix='v2')
    print(app.url_map)


def create_app():
    app = Flask(__name__)
    configure_app(app)
    register_extensions(app)
    register_blueprints(app)

    return app
